package com.qf.controller;

import com.qf.pojo.Admin;
import com.qf.pojo.PageBean;
import com.qf.pojo.Product;
import com.qf.pojo.User;
import com.qf.service.AdminService;
import com.qf.service.ProductService;
import com.qf.service.impl.AdminServiceImpl;
import com.qf.service.impl.ProductServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

/**
 * 管理员后端接口地址
 */
@WebServlet("/admin")
public class AdminServlet extends BaseServlet {


    /**
     * 管理员登录功能
     * @param request 请求对象
     * @param response 响应对象
     */
    public void admin_login(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {


        //解决post中文乱码
        //  request.setCharacterEncoding("utf-8");

        //1)接收前端参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // System.out.println(username+"----"+password);
        //2)封装管理员实体
        Admin admin = new Admin() ;
        admin.setUsername(username) ;
        admin.setPassword(password) ;
        //3)调用管理员的业务接口
        AdminService adminService = new AdminServiceImpl() ;
        Admin loginAdmin = adminService.getAdmin(admin.getUsername(), admin.getPassword());

        if(loginAdmin!=null){
            //用户登录的时候,要将用户的数据放在服务器端
            //将用户的数据存储在HttpSession (会话对象)
            //获取HttpSession对象?
            //HttpServletRequest---->public HttpSession getSession()
            //创建HttpSession会话对象
            HttpSession session = request.getSession();
            //String id = session.getId();
            //System.out.println(id) ;
            //管理员名字存进去
            //session的钝化 (会给磁盘上永久写一个数据 存储在.ser文件中)
            session.setAttribute("loginAdmin",loginAdmin);
            //登录成功,跳转到主页
            //重定向:仅仅页面跳转
            response.sendRedirect(request.getContextPath()+"/admin/admin_index.jsp");

        }else {
            //没有查询到
            request.setAttribute("msg","用户名或者密码错误");
            //请求转发到admin/admin_login.jsp
            request.getRequestDispatcher("/admin/admin_login.jsp").forward(request,response);
        }

    }

    /**
     * 管理员退出
     * @param request 请求对象
     * @param response 响应对象
     */
    public void adminLogout(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //从请求对象获取HttpSession  会话对象
        HttpSession session = request.getSession();
        if(session!=null){
            //public void invalidate()
            //使此会话无效，然后取消对任何绑定到它的对象的绑定
            session.invalidate(); //设置session会话无效
            //重定向到管理员主页
            response.sendRedirect(request.getContextPath()+"/admin/admin_index.jsp");
        }
    }

    /**
     * 后台管理员查询所有用户(分页查询)
     * @param request 请求对象
     * @param response 响应对象
     */
    public void adminFindUserPage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //1)接收查询用户的参数
        //当前页码
        String curPage = request.getParameter("currentPage");
        //如果当前地址栏这个参数是null或者它空字符串,给curPage默认值
        if(curPage==null || "".equals(curPage)){
            curPage = "1" ; //第一页
        }
        //每页显示的条数
        String pSize = request.getParameter("pageSize");
        if(pSize==null || "".equals(pSize)){
            pSize = "3"  ;//每页显示2条
        }

        //2)将curPage和pSize转换成int
        int currentPage = Integer.parseInt(curPage);
        int pageSize = Integer.parseInt(pSize) ;

        //3)调用针管理员的业务接口
        AdminService adminService = new AdminServiceImpl() ;
        PageBean<User> pb = adminService.findUserByPage(currentPage, pageSize);
        //System.out.println(pb) ;

        //4)将PageBean对象存储在request域中
        request.setAttribute("pb",pb) ;
        //5)请求转发user/user_list.jsp
        request.getRequestDispatcher("/user/user_list.jsp").forward(request,response);

    }

    /**
     * 管理员查询指定用户的功能
     * @param request  请求对象
     * @param response 响应递归性
     */
    public void adminFindUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        //<a href="${pageContext.request.contextPath}/admin?methodName=adminFindUser&uid=${u.uid}">修改</a>
        //1)接收点击修改 ----uid的这个参数
        String uid = request.getParameter("uid");
        // System.out.println(uid);
        //2)调用管理员业务接口
        AdminService adminService = new AdminServiceImpl() ;
        User user = adminService.findUserByUid(uid);
        if(user!=null){
            //将user对象存储在request域中
            request.setAttribute("user",user) ;
            //请求转发到/user/update_user.jsp
            request.getRequestDispatcher("/user/update_user.jsp").forward(request,response);
        }
    }

    /**
     * 管理员修改指定用户的功能
     * @param request 请求对象
     * @param response 响应对象
     */
    public void adminUpdateUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {


        //解决post提交的中文乱码
        //  request.setCharacterEncoding("utf-8") ;
        //1)接收表单提交的参数
        String uid = request.getParameter("uid");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String sex = request.getParameter("sex");
        //System.out.println(uid+"---"+username+"---"+password+"---"+name+"---"+email+"---"+sex) ;

        //2)封装User对象
        User user = new User() ;
        user.setUid(uid) ;
        user.setUsername(username) ;
        user.setPassword(password) ;
        user.setName(name) ;
        user.setEmail(email) ;
        user.setSex(sex);

        //3)调用业务接口
        AdminService adminService = new AdminServiceImpl() ;
        adminService.update(user);

        //4)请求转发到后台里面查询所有用户的操作
        request.getRequestDispatcher("/admin?methodName=adminFindUserPage").forward(request,response);

    }

    /**
     * 管理员删除指定的用户
     * @param request 请求
     * @param response 响应对象
     */
    public void adminDelUser(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //window.location.href="${pageContext.request.contextPath}/admin?methodName=adminDelUser&uid="+uid;  //地址栏携带参数
        //1)接收参数 uid
        String uid = request.getParameter("uid");
        //System.out.println(uid);

        //2)调用业务接口
        AdminService adminService = new AdminServiceImpl() ;
        //删除用户
        adminService.deleteUser(uid) ;

        //3)请求转发到查询所有的后台地址上
        request.getRequestDispatcher("/admin?methodName=adminFindUserPage").forward(request,response);

    }

    /**
     * 分页查询所有商品
     * @param request
     * @param response
     */
    public void adminFindAllProduct(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进来了");
        ProductService productService = new ProductServiceImpl() ;
        List<Product> list =  productService.findPageProduct() ;

        request.setAttribute("list",list);
        request.getRequestDispatcher("/admin/product/product_list.jsp").forward(request,response);
    }
}
